package com.wenhai.disruptor.producer;

import com.lmax.disruptor.RingBuffer;
import com.wenhai.disruptor.event.OrderEvent;

import java.nio.ByteBuffer;

/**
 *
 *
 * @author wenhai
 * @date   2021/1/12
 */
public class OrderEventProducer {
    private final  RingBuffer<OrderEvent> ringBuffer;
    public OrderEventProducer(RingBuffer<OrderEvent> ringBuffer) {
        this.ringBuffer = ringBuffer;
    }

    public void sendData(ByteBuffer data) {
        long sequence = ringBuffer.next();
        try {
            OrderEvent event = ringBuffer.get(sequence);
            event.setId(data.getLong(0));
        } finally {
            ringBuffer.publish(sequence);
        }
    }
}
